﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class manager_user : System.Web.UI.Page
{   
    private string txtEmail, txtName, txtPass, txtCodeLogin, txtDatestart, txtExam, txtRound, txtTimestart;
    private SQLQuery sqlQuery = null;
    private Connection myConnection = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("../index.htm");
        }
        myConnection = new Connection();
        if (!myConnection.isConnectionDatabase())
        {
            Response.Redirect("../disconnect.htm");
        }

        sqlQuery = new SQLQuery();
        if (!IsPostBack)
        {
            showGridView();
            fillDateToDropDownListExam();
            fillDataToGridViewCandidate();
            fillDataToGridViewTests();
        }
    }

    private void showGridView()
    {
        layout_gridview.Visible = true;
        layout_infor.Visible = false;
        layout_details.Visible = false;
    }

    private void showLayoutInfor()
    {
        layout_infor.Visible = true;
        layout_gridview.Visible = false;        
        layout_details.Visible = false;
    }

    private void fillDataToGridViewCandidate()
    {
        sqlQuery.loadDataGridView(mGridView_Candidate, LocalVariables.SQLQUERY_SELECT_TABLE_CANDIDATE);
    }

    private void fillDataToGridViewTests()
    {
        sqlQuery.loadDataGridView(mGridView_Test, LocalVariables.SQLQUERY_SELECT_TABLE_TESTS);
    }

    private void fillDateToDropDownListExam()
    {
        sqlQuery.loadExamListData(ddl_exam);
    }

    protected void mGridView_Test_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbl_msg.Text = "";
        lbl_candidate.Text = "";
        if (!mGridView_Test.SelectedRow.Cells[8].Text.Trim().Equals("waiting"))
        {
            lbl_not_data.Text = LocalVariables.ROUND_IS_CLOSED;
            return;
        }
        layout_search_candidate.Visible = false;
        showLayoutInfor();
        btn_edit.Visible = true;
        btn_save.Visible = false;
        btn_reset.Visible = false;

        txt_email.Text = mGridView_Test.SelectedRow.Cells[1].Text.Trim();
        txt_code_login.Text = mGridView_Test.SelectedRow.Cells[2].Text.Trim();
        txt_pass.Text = mGridView_Test.SelectedRow.Cells[3].Text.Trim();
        ddl_exam.Text = mGridView_Test.SelectedRow.Cells[4].Text.Trim();
        txt_round.Text = mGridView_Test.SelectedRow.Cells[5].Text.Trim();
        bdp_datestart.SelectedValue = mGridView_Test.SelectedRow.Cells[6].Text.Trim();
        ddl_timestart.Text = mGridView_Test.SelectedRow.Cells[7].Text.Trim();
    }

    protected void mGridView_Test_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        mGridView_Test.PageIndex = e.NewPageIndex;
        fillDataToGridViewTests();
    }

    protected void mGridView_Candidate_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbl_candidate.Text = "";
        txtName = mGridView_Candidate.SelectedRow.Cells[0].Text.ToString().Trim();
        txtEmail = mGridView_Candidate.SelectedRow.Cells[1].Text.ToString().Trim();
        int round = sqlQuery.getRoundCount(txtEmail);
        if (round > 3)
        {
            lbl_candidate.Text = "Candidate \"" + txtName + "\" surpassed all";
            clearText();
            return;
        }
        if (round > 1)
        {
            lbl_candidate.Text = "Candidate \"" + txtName + "\" passed round : " + (round - 1);
        }        
        txt_email.Text = txtEmail;
        txt_code_login.Text = mGridView_Candidate.SelectedRow.Cells[0].Text.ToString().Trim().Replace(" ","");        
        txt_round.Text = Convert.ToString(round);
    }

    protected void mGridView_Candidate_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        mGridView_Candidate.PageIndex = e.NewPageIndex;
        fillDataToGridViewCandidate();
    }

    protected void btn_reset_Click(object sender, EventArgs e)
    {
        lbl_candidate.Text = "";
        clearText();
        fillDataToGridViewCandidate();
    }

    private void clearText()
    {
        txt_search_candadite.Text = "";
        txt_email.Text = "";
        txt_pass.Text = "";
        txt_code_login.Text = "";
        txt_round.Text = "";
        lbl_msg.Text = "";
        bdp_datestart.SelectedValue = "";
        ddl_exam.SelectedIndex = 0;
        ddl_timestart.SelectedIndex = 0;
    }

    private bool getText()
    {
        txtEmail = txt_email.Text.Trim();
        if (LocalVariables.isNullorEmpty(txtEmail))
        {
            lbl_msg.Text = LocalVariables.EMAIL_FAILURE;
            return false;
        }        
        txtCodeLogin = txt_code_login.Text.Trim();
        if (LocalVariables.isNullorEmpty(txtCodeLogin))
        {
            lbl_msg.Text = LocalVariables.CODE_LOGIN_FAILURE;
            return false;
        }
        txtPass = txt_pass.Text.Trim();
        if (LocalVariables.isNullorEmpty(txtPass))
        {
            lbl_msg.Text = LocalVariables.PASSWORD_FAILURE;
            return false;
        }
        txtRound = txt_round.Text.Trim();
        if(ddl_exam.SelectedIndex == 0)
        {
            lbl_msg.Text = LocalVariables.CHOOSE_DATE_EXAM;
            return false;
        }
        txtExam = ddl_exam.Text.Trim();
        txtDatestart = bdp_datestart.Text.Trim();        
        if (LocalVariables.isNullorEmpty(txtDatestart))
        {
            lbl_msg.Text = LocalVariables.CHOOSE_DATE_TEST;
            return false;
        }
        else
        {
            int currentdate = LocalVariables.getCurrentDate();
            int dateselect = Convert.ToInt32(txtDatestart.Replace("-", ""));
            if (dateselect <= currentdate)
            {
                lbl_msg.Text = LocalVariables.CHECK_DATE_TEST;
                return false;
            }
        }        
        txtTimestart = ddl_timestart.Text.Trim();
        lbl_msg.Text = "";
        return true;
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (getText())
        {
            if (sqlQuery.selectExamIsExits(txtExam))
            {
                if (sqlQuery.insertTableTest(txtEmail, txtCodeLogin, txtPass, txtRound, txtExam, txtDatestart, txtTimestart))
                {
                    sendMailForCandidate();
                    lbl_not_data.Text = LocalVariables.ADD_ROUND_SUCCESS;
                    fillDataToGridViewTests();
                    showGridView();
                    clearText();
                }
            }
            else
            {
                lbl_msg.Text = LocalVariables.EXAM_NO_QUESTION;
            }
        }
    }

    protected void btn_add_Click(object sender, EventArgs e)
    {
        lbl_msg.Text = "";
        lbl_candidate.Text = "";
        clearText();
        showLayoutInfor();
        layout_search_candidate.Visible = true;
        btn_edit.Visible = false;
        btn_save.Visible = true;
        btn_reset.Visible = true;
    }

    protected void btn_del_Click(object sender, EventArgs e)
    {
        string email = "", round = "";
        try
        {
            HtmlInputCheckBox chk = new HtmlInputCheckBox();
            for (int i = 0; i < mGridView_Test.Rows.Count; i++)
            {
                email = mGridView_Test.Rows[i].Cells[1].Text.Trim();
                round = mGridView_Test.Rows[i].Cells[5].Text.Trim();
                chk = (HtmlInputCheckBox)mGridView_Test.Rows[i].FindControl("chk");
                if (chk.Checked == true)
                {
                    sqlQuery.deleteTableTest(email, round);
                }
            }
            fillDataToGridViewTests();
        }
        catch { }
    }

    protected void btn_refresh_Click(object sender, EventArgs e)
    {
        txt_search_test.Text = "";
        lbl_not_data.Text = "";
        fillDataToGridViewTests();
    }

    protected void btn_back_Click(object sender, EventArgs e)
    {
        lbl_not_data.Text = "";
        lbl_candidate.Text = "";
        showGridView();
    }

    protected void btn_edit_Click(object sender, EventArgs e)
    {
        if (getText())
        {
            if (sqlQuery.selectExamIsExits(txtExam))
            {

                if (sqlQuery.updateTableTest(txtEmail, txtCodeLogin, txtPass, txtRound, txtExam, txtDatestart, txtTimestart))
                {
                    sendMailForCandidate();
                    lbl_not_data.Text = LocalVariables.EDIT_ROUND_SUCCESS;
                    fillDataToGridViewTests();
                    showGridView();
                    clearText();
                }
            }
            else
            {
                lbl_msg.Text = LocalVariables.EXAM_NO_QUESTION;
            }
        }
    }

    protected void mGridView_Test_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ViewCandidate")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            txtEmail = mGridView_Test.Rows[index].Cells[1].Text.Trim();
            lbl_email.Text = txtEmail;
            string[] ARRAY = sqlQuery.selectInforItemTests(txtEmail, mGridView_Test.Rows[index].Cells[5].Text.Trim());
            if (ARRAY != null)
            {
                clearLabel();
                layout_details.Visible = true;
                layout_gridview.Visible = false;
                lbl_codelgoin.Text = ARRAY[0].ToString();
                lbl_pass.Text = ARRAY[1].ToString();
                lbl_round.Text = ARRAY[2].ToString();
                lbl_datetest.Text = ARRAY[3].ToString();
                lbl_timestart.Text = ARRAY[4].ToString();
                lbl_examtest.Text = ARRAY[5].ToString();
                if (!ARRAY[7].Trim().Equals("") || ARRAY[7] == null)
                {
                    lbl_answer.Text = ARRAY[7].ToString() + "/10";
                }
                lbl_timecompleted.Text = ARRAY[8].ToString();
                lbl_results.Text = ARRAY[9].ToString();
                if (!ARRAY[9].Trim().Equals("waiting"))
                {
                    sqlQuery.viewQuestionResult(mGridView_question, txtEmail, ARRAY[5].ToString(), ARRAY[2].ToString());
                    mGridView_question.Visible = true;
                }
                else
                {
                    mGridView_question.Visible = false;
                }
            }
        }
    }

    protected void btn_details_back_Click(object sender, EventArgs e)
    {
        layout_details.Visible = false;
        layout_gridview.Visible = true;
        clearLabel();
    }

    private void clearLabel()
    {
        lbl_codelgoin.Text = "";
        lbl_pass.Text = "";
        lbl_round.Text = "";
        lbl_datetest.Text = "";
        lbl_timestart.Text = "";
        lbl_examtest.Text = "";
        lbl_answer.Text = "";
        lbl_results.Text = "";
        lbl_timecompleted.Text = "";
    }

    protected void btn_search_candadite_Click(object sender, EventArgs e)
    {
        string value = txt_search_candadite.Text.Trim();
        if (LocalVariables.isNullorEmpty(value))
        {
            lbl_candidate.Text = LocalVariables.KEYWORD_FAILURE;
            txt_search_candadite.Focus();
        }
        else
        {
            lbl_candidate.Text = "";
            if (sqlQuery.searchInforTableCandidate(mGridView_Candidate, value))
            {
                lbl_candidate.Text = LocalVariables.SEARCH_NOT_RESULTS;
            }
        }
    }

    protected void btn_search_test_Click(object sender, EventArgs e)
    {
        string value = txt_search_test.Text.Trim();
        if (LocalVariables.isNullorEmpty(value))
        {
            lbl_not_data.Text = LocalVariables.KEYWORD_FAILURE;
            txt_search_test.Focus();
        }
        else
        {
            lbl_not_data.Text = "";
            if (sqlQuery.searchInforTableTest(mGridView_Test, value))
            {
                lbl_not_data.Text = LocalVariables.SEARCH_NOT_RESULTS;
            }
        }
    }

    private void sendMailForCandidate()
    {
        string title = "Hi you invites to test round " + txtRound;
        string content = "";
        content += "Exam Test  : " + txtExam + "<br/>";
        content += "Code Login : " + txtCodeLogin + "<br/>";
        content += "Password   : " + txtPass + "<br/>";
        content += "Date Test  : " + txtDatestart + "<br/>";
        content += "Time Start : " + txtTimestart;        
        MailMessages.sendMail(txtEmail, title, content);
    }
}